import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import {userInfo,buyCarMsg} from '../api/Tlmusic'

export default new Vuex.Store({
  state: {
    isLogin:false,
    userInfo:{},
    carNum:0,
    buyCarMsg:[],
    isAddCar:false,
    flag:false
  },
  mutations: {
    changelogin(state,payload){
      state.isLogin = payload.isLogin
      state.userInfo = payload.userInfo || {};
    },
    addCarNum(state,payload){
      state.carNum = payload.carNum
      state.buyCarMsg = payload.buyCarMsg || [];
      state.isAddCar = payload.isAddCar
    },
    alertBuyCar(state,payload){
      state.flag = payload.flag
    }
  },
  actions: {
    //获取用户信息，判断是否登录
    getUserInfo({commit}){
      return userInfo().then(res => {
        // console.log(res)
        const payload = {
          isLogin: false,
          userInfo: {}
        };
        if (res.code === 0) {
          payload.isLogin = true;
          payload.userInfo = res.userInfo;
        }
        commit("changelogin", payload);
        return res;
      })
    },
    //获取用户信息，判断是否登录
    getBuyCarInfo({commit}){
      return buyCarMsg().then(res => {
        // console.log(res)
        const payload = {
          carNum:0,
          buyCarMsg:[],
          isAddCar:false,
        };
        if (res.code === 0) {
          payload.carNum = res.shoppingCartList.length;
          payload.buyCarMsg = res.shoppingCartList;
          payload.isAddCar = true;
        }
        commit("addCarNum", payload);
        return res;
      })
    }
  },
  modules: {
  }
})
